
#include "IO.h"
#include "BSP_Delay.h"
#include "BSP_Config.h"
#include "gd32f30x.h"


static const IO_T wgInD0 =
{
	(void *)
	WG_IN_D0_GP, WG_IN_D0_PIN
};


static const IO_T wgInD1 =
{
	(void *)
	WG_IN_D1_GP, WG_IN_D1_PIN
};


static void wgDummyCallback(uint8_t b)
{

}


static void(*wgInCallback) (uint8_t bit) = wgDummyCallback;

//WG接收中断
void EXTI5_9_IRQHandler(void)
{
	
	if (RESET != exti_interrupt_flag_get(EXTI_6)) {
		exti_interrupt_flag_clear(EXTI_6);
		if(!IO_Get(&wgInD0)){
			wgInCallback(0);
		}
	}

	if (RESET != exti_interrupt_flag_get(EXTI_7)) {
		exti_interrupt_flag_clear(EXTI_7);
		if(!IO_Get(&wgInD1)){
			wgInCallback(1);
		}
	}
}

int BSP_WG_In_Get(uint8_t num){
	if(num){
		return IO_Get(&wgInD1);
	}
	return IO_Get(&wgInD0);
}


void BSP_WG_In_Init(void)
{
	IO_Init(&wgInD0, IO_Mode_IPU);
	IO_Init(&wgInD1, IO_Mode_IPU);

	nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
	nvic_irq_enable(EXTI5_9_IRQn, 2U, 0U);
	/* connect key EXTI line to key GPIO pin */
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_6);
	/* configure key EXTI line */
	exti_init(EXTI_6, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
	exti_interrupt_flag_clear(EXTI_6);

	/* connect key EXTI line to key GPIO pin */
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_7);
	/* configure key EXTI line */
	exti_init(EXTI_7, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
	exti_interrupt_flag_clear(EXTI_7);
}


void BSP_WG_In_SetCallback(void(*cb) (uint8_t bit))
{
	wgInCallback = cb;
}



